package com.mbc.web.common.client.ui;

import java.util.List;

import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.user.client.ui.Composite;

public abstract class AbstractListView<T> extends Composite {
	protected T selectedEntity;
	protected String title;
	protected List<T> dataList;
	public T getSelectedEntity(){
		return selectedEntity;
	}
	protected boolean isModal = false;
	protected abstract void initialize();
	protected abstract void bindDisplay(List<T> list);
	protected abstract void search();
	
	/**
	 * Show Modal Dialog
	 * @param closeHandler - CloseHandler<Modal>
	 */
	public void showDialog(CloseHandler<Modal> closeHandler){
		Modal l_Modal = new Modal();
		l_Modal.setTitle(this.title);
		isModal = true;
		l_Modal.addCloseHandler(closeHandler);
		l_Modal.show(this);
		clearHeader();
	}
	
	/**
	 * Show Modal Dialog
	 * @param closeHandler - CloseHandler<Modal>
	 * @param size - Modal.MODAL_LARGE | Modal.MODAL_SMALL
	 */
	public void showDialog(CloseHandler<Modal> closeHandler, int size){
		Modal l_Modal = new Modal();
		l_Modal.setTitle(this.title);
		isModal = true;
		l_Modal.addCloseHandler(closeHandler);
		l_Modal.show(this, size);
		clearHeader();
	}
	
	private native void clearHeader()/*-{
		$wnd.$("#colDiv").attr("class","col-sm-12");
		$wnd.$(".modal-hide").hide();
	}-*/;
	
	protected native void closeModal() /*-{
	$wnd.$('#myModal').modal('hide');
	}-*/;
	
	protected native void generateList()/*-{
		$wnd.$("#listViewDiv>div").remove();
		var $_headers = $wnd.$(".table th");
		var $_divListGroup = $wnd.$("<div/>").addClass("list-group").appendTo("#listViewDiv");
		$wnd.$(".table tbody tr").each(function(){
				var i = 0;
				var $_a = $wnd.$("<a/>").addClass("list-group-item").appendTo($_divListGroup).css("cursor","pointer").click(function(){
					$wnd.$(".table tbody tr:nth-child(" + ($wnd.parseInt($wnd.$(this).index()) + 1) + ") td:first-child div").trigger("click");
				});
				$wnd.$(this).children().each(function(){
					var l_text = $wnd.$($_headers[i]).text() + " : " + $wnd.$(this).text(); 
					if(i == 0)
						$wnd.$("<h4/>").addClass("list-group-item-heading").text(l_text).appendTo($_a);
					else
						$wnd.$("<p/>").addClass("list-group-item-text").text(l_text ).appendTo($_a);
					i++;
				});
			}
		);
	}-*/;
}
